<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Function pointers</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_datatypes_funcptr.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Function pointers </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>A function pointer is a data type that can be dynamically set to point to a global function that has a matching function signature as that defined by the variable declaration. Function pointers are commonly used for callbacks, i.e. where a piece of code must be able to call back to some code based on some conditions, but the code that needs to be called is not known at compile time.</p>
<p>To use function pointers it is first necessary to <a class="el" href="doc_global.html#doc_global_funcdef">define the function signature</a> that will be used at the global scope. Once that is done the variables can be declared using that definition.</p>
<p>Here's an example that shows the syntax for using function pointes:</p>
<pre>
  // Define a function signature for the function pointer
  funcdef bool CALLBACK(int, int);</pre><pre>  // An example function that shows how to use this
  void main()
  {
    // Declare a function pointer, and set it 
    // to point to the myCompare function.
    CALLBACK @func = @myCompare;</pre><pre>    // The function pointer can be compared with the 'is' operator
    if( func is null )
    {
      print("The function pointer is null\n");
      return;
    }</pre><pre>    // Call the function through the pointer, just as if it was a normal function
    if( func(1, 2) )
    {
      print("The function returned true\n");
    }
    else
    {
      print("The function returned false\n");
    }
  }</pre><pre>  // This function matches the CALLBACK definition, since it has 
  // the same return type and parameter types.
  bool myCompare(int a, int b)
  {
    return a &gt; b;
  }
</pre> </div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:04 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
